<template>
  <view class="dashboard-page">
    <view class="header">
      <text class="title">数字孪生驾驶舱</text>
    </view>
    
    <scroll-view class="content" scroll-y>
      <!-- 实时KPI -->
      <view class="section">
        <view class="section-title">实时业务指标</view>
        <view class="kpi-grid">
          <view 
            v-for="kpi in kpiList" 
            :key="kpi.key"
            class="kpi-card"
          >
            <text class="kpi-label">{{ kpi.label }}</text>
            <text class="kpi-value">{{ kpi.value }}</text>
            <text class="kpi-unit">{{ kpi.unit }}</text>
          </view>
        </view>
      </view>
      
      <!-- 业务模拟 -->
      <view class="section">
        <view class="section-title">What-If 分析</view>
        <button class="action-btn" @click="runSimulation">运行业务模拟</button>
      </view>
      
      <!-- 模拟结果 -->
      <view v-if="simulationResult" class="section">
        <view class="section-title">模拟结果</view>
        <view class="result-card">
          <view 
            v-for="(value, key) in simulationResult.predictedMetrics"
            :key="key"
            class="metric-row"
          >
            <text class="metric-label">{{ formatMetricName(key) }}</text>
            <text class="metric-value">{{ value }}</text>
          </view>
        </view>
        
        <view class="recommendations">
          <text class="rec-title">AI建议：</text>
          <view 
            v-for="(rec, idx) in simulationResult.recommendations"
            :key="idx"
            class="rec-item"
          >
            <text>{{ idx + 1 }}. {{ rec }}</text>
          </view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import request from '../../utils/request'

interface KPI {
  key: string
  label: string
  value: number | string
  unit: string
}

const kpiList = ref<KPI[]>([
  { key: 'messages', label: '消息量', value: '加载中...', unit: '' },
  { key: 'files', label: '文件上传', value: '加载中...', unit: '' },
  { key: 'calls', label: '通话时长', value: '加载中...', unit: '' },
  { key: 'orders', label: '仓储订单', value: '加载中...', unit: '' }
])

const simulationResult = ref<any>(null)

onMounted(() => {
  loadRealTimeKPIs()
})

const loadRealTimeKPIs = async () => {
  try {
    const data = await request<any>({
      url: '/api/digital-twin/kpis/realtime'
    })
    
    kpiList.value = [
      { key: 'messages', label: '小时消息量', value: Math.round(data.hourly_messages || 0), unit: '条' },
      { key: 'files', label: '小时文件', value: Math.round(data.hourly_files || 0), unit: '个' },
      { key: 'calls', label: '小时通话', value: Math.round(data.hourly_call_minutes || 0), unit: '分钟' },
      { key: 'orders', label: '小时订单', value: Math.round(data.hourly_warehouse_orders || 0), unit: '单' }
    ]
  } catch (error) {
    console.error('加载KPI失败', error)
  }
}

const runSimulation = async () => {
  uni.showLoading({ title: '模拟中...' })
  
  try {
    const data = await request<any>({
      url: '/api/digital-twin/simulation/what-if',
      method: 'POST',
      data: {
        name: '增加20%订单量',
        parameters: {
          orderIncrease: 0.2
        }
      }
    })
    
    simulationResult.value = data
    uni.hideLoading()
  } catch (error) {
    uni.hideLoading()
    console.error('模拟失败', error)
  }
}

const formatMetricName = (key: string): string => {
  const names: Record<string, string> = {
    throughput: '吞吐量',
    responseTime: '响应时间',
    errorRate: '错误率'
  }
  return names[key] || key
}
</script>

<style scoped>
.dashboard-page {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #f5f5f5;
}

.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 60rpx 40rpx;
  color: white;
}

.title {
  font-size: 44rpx;
  font-weight: bold;
}

.content {
  flex: 1;
  padding: 30rpx;
}

.section {
  margin-bottom: 40rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 24rpx;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 24rpx;
}

.kpi-card {
  background: white;
  padding: 40rpx 30rpx;
  border-radius: 20rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.kpi-label {
  font-size: 26rpx;
  color: #666;
  margin-bottom: 16rpx;
}

.kpi-value {
  font-size: 48rpx;
  font-weight: bold;
  color: #667eea;
  margin-bottom: 8rpx;
}

.kpi-unit {
  font-size: 24rpx;
  color: #999;
}

.action-btn {
  width: 100%;
  height: 90rpx;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 20rpx;
  font-size: 32rpx;
  border: none;
}

.result-card {
  background: white;
  padding: 30rpx;
  border-radius: 20rpx;
  margin-bottom: 24rpx;
}

.metric-row {
  display: flex;
  justify-content: space-between;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f5f5f5;
}

.metric-row:last-child {
  border-bottom: none;
}

.metric-label {
  font-size: 28rpx;
  color: #666;
}

.metric-value {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.recommendations {
  background: #fff9e6;
  padding: 30rpx;
  border-radius: 20rpx;
}

.rec-title {
  display: block;
  font-size: 28rpx;
  font-weight: bold;
  color: #ff9800;
  margin-bottom: 16rpx;
}

.rec-item {
  margin-bottom: 12rpx;
  font-size: 26rpx;
  color: #666;
}
</style>
